import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/app',
      name: '',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/app/index',
      children: [
        {
          path: 'index',
          name: 'index',
          component: () => import('@/views/index/index.vue')
        }
      ,
        {
          path: 'class',
          name: 'class',
          component: () => import('@/views/class/index.vue')
        },
        {
          path: 'person',
          name: 'person',
          component: () => import('@/views/personnel/index.vue')
        },
        {
          path: 'dept',
          name: 'dept',
          component: () => import('@/views/dept/index.vue')
        },
        {
          path: 'people',
          name: 'people',
          component: () => import('@/views/people/index.vue')
        },
         {
          path: 'role',
          name: 'role',
          component: () => import('@/views/role/index.vue')
         }
         ,
         {
          path: 'menu',
          name: 'menu',
          component: () => import('@/views/menu/index.vue')
        }
      ]
    },
    {
      path: '/',
      name: 'login',
      component: () => import('@/views/login/index.vue')
    }
  ]
})
/**
 * 登录校验（全局路由守卫）
 */
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const whiteList = ['/'] // 白名单：登录页

  if (!token && !whiteList.includes(to.path)) {
    // 没有 token 且访问的不是白名单页面 -> 跳转到登录页
    next('/')
  } else {
    // 放行
    next()
  }
})

export default router
